package io.plus.client;

import io.plus.interactive.AsynchronousSocketChannelContext;
import io.plus.interactive.Configuration;
import io.plus.interactive.user_interface.AioHandler;
import io.plus.utils.lock.ReadWriteLockSet;
import io.plus.utils.lock_capable_task.LockCapableTaskThreadPoolExecutor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.HashSet;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Data
public class ClientConfiguration extends Configuration {
    public final ReadWriteLockSet<AsynchronousSocketChannelContext> connecteds = new ReadWriteLockSet<AsynchronousSocketChannelContext>(new HashSet<AsynchronousSocketChannelContext>());
    public final ReadWriteLockSet<AsynchronousSocketChannelContext> closeds = new ReadWriteLockSet<AsynchronousSocketChannelContext>(new HashSet<AsynchronousSocketChannelContext>());
    protected ReConnectConfiguration reConnectConfiguration;//重连配置
    private ClientAioHandler clientAioHandler = null;
    private ConnectionCompletionHandler connectionCompletionHandler = new ConnectionCompletionHandler();

    /**
     * 不重连
     */
    public ClientConfiguration(ClientAioHandler aioHandler) {
        this(aioHandler, null);
    }

    /**
     * @param aioHandler
     * @param reConnectConfiguration 不用框架自动重连，就传null
     */
    public ClientConfiguration(ClientAioHandler aioHandler, ReConnectConfiguration reConnectConfiguration) {
        this(aioHandler, reConnectConfiguration, null, null);
    }

    /**
     * @param clientAioHandler
     * @param clientAioListener
     * @param reConnectConfiguration 不用框架自动重连，就传null
     * @param tioExecutor
     * @param groupExecutor
     */
    public ClientConfiguration(ClientAioHandler clientAioHandler, ReConnectConfiguration reConnectConfiguration, LockCapableTaskThreadPoolExecutor tioExecutor,
                               ThreadPoolExecutor groupExecutor) {
        super(tioExecutor, groupExecutor);

        this.setClientAioHandler(clientAioHandler);
        this.reConnectConfiguration = reConnectConfiguration;
    }

    @Override
    public AioHandler getAioHandler() {
        return this.getClientAioHandler();
    }


    @Override
    public boolean isServer() {
        return false;
    }

    @Override
    public String toString() {
        return "ClientConfiguration [name=" + name + "]";
    }
}
